package vectors;

import java.util.Iterator;
import java.util.LinkedList;

//TODO см. JArrayListVector

public class JLinkedListVector implements Vector {
	private static final long serialVersionUID = 2743870521735563437L;
	private LinkedList<Double> elements;


	public JLinkedListVector() {
		elements = new LinkedList<Double>();
	}

	public JLinkedListVector(double... values) {
		elements = new LinkedList<Double>();
		for (int i = 0; i < values.length; i++) {
			addElement(values[i]);
		}
	}

	public void addElement(Double value) {
		elements.add(elements.size(), value);
	}

	public void deleteElement() {
		elements.remove(elements.size() - 1);
	}

	@Override
	public double getElement(int index) {
		return elements.get(index);
	}

	@Override
	public void setElement(int index, double value) {
		elements.set(index, value);
	}

	@Override
	public int getLength() {
		return elements.size();
	}

	@Override
	public void fillFromAnArray(double[] source) {
		for (int i = 0; i < source.length; i++) {
			elements.add(source[i]);
		}
	}

	@Override
	public void fillFromAnotherVector(Vector source) {
		for (int i = 0; i < source.getLength(); i++) {
			addElement(source.getElement(i));
		}
	}

	@Override
	public void multiplyByNumber(double number) {
		for (int i = 0; i < elements.size(); i++) {
			Double newValue = getElement(i) * number;
			setElement(i, newValue);
		}
	}

	@Override
	public void add(Vector v) {
		if (elements.size() != v.getLength()) {
			System.out.println("Vectors can't be added.\n");
			return;
		}
		for (int i = 0; i < elements.size(); i++) {
			Double newValue = getElement(i) + v.getElement(i);
			setElement(i, newValue);
		}
	}

	@Override
	public void compare(Vector v) {
		if (elements.size() != v.getLength()) {
			System.out.println("Vectors are different.\n");
			return;
		}
		for (int i = 0; i < elements.size(); i++) {
			if (getElement(i) != v.getElement(i)) {
				System.out.println("Vectors are different.\n");
				return;
			}
		}
		System.out.println("Vectors are equal.\n");
	}

	@Override
	public String toString() {
		StringBuffer s = new StringBuffer("JArrayListVector containing ");
		s.append(elements.size());
		s.append(" elements which are:\n");
		for (int i = 0; i < getLength(); i++) {
			s.append(elements.get(i));
			s.append('\n');
		}
		return s.toString();
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Vector)) {
			return false;
		}
		Vector v = (Vector) obj;
		if (elements.size() != v.getLength()) {
			return false;
		}
		for (int i = 0; i < elements.size(); i++) {
			if (elements.get(i) != v.getElement(i)) {
				return false;
			}
		}
		return true;
	}

	@Override
	public int hashCode() {
		int hashCode = 0;
		for (int i = 0; i < elements.size(); i++) {
			hashCode ^= elements.get(i).hashCode();
		}
		return hashCode;
	}

	@Override
	public Object clone() {
		JLinkedListVector result = null;
		try {
			result = (JLinkedListVector) super.clone();
			result.elements = (LinkedList<Double>) this.elements.clone();
		} catch (CloneNotSupportedException ex) {
		}
		return result;
	}

	@Override
	public Iterator<Double> iterator() {
		return elements.iterator();
	}

	public void printElements() {
		System.out.println("Vector has " + elements.size() + " elements.");
		for (int i = 0; i < elements.size(); i++) {
			System.out.println(elements.get(i));
		}
		System.out.println();
	}
}
